// 验证码图片验证demo

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var svgCaptcha = require('svg-captcha');
var text = '';//验证码

var urlencodedParser = bodyParser.urlencoded({ extended: false });// 创建 application/x-www-form-urlencoded 编码解析


app.all('/',(req,res,next)=>{
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
    next();
});
app.post('/svg',urlencodedParser,(req,res)=>{
    const  captcha = svgCaptcha.create({
        size:6,
        ignoreChars:'o01i',
        noise:2,
        color: true, // 验证码的字符是否有颜色，默认没有，如果设定了背景，则默认有
        background:'#cc9966'
    });
    // req.session.captcha = captcha.text; // session 存储
    text = captcha.text;
    res.type('svg');
    res.status(200).send(captcha.data);
});
app.post('/verify',urlencodedParser,(req,res)=>{
  let code = req.body.code;
  if(code === text){
      res.send('验证成功！');
  }else{
      res.send('验证失败!');
  }
});
var server = app.listen(3000,()=>{
    var host = server.address().address
    var port = server.address().port
    console.log('成功监听',host,port);
})